<?php

class Empeval_Notifications_Rejection {
	
	private $config;
	private $enabled;
	
	public function __construct() {
		$this->config = Empeval_Config::getInstance()->notifications->rejection;
		$this->enabled = (bool)$this->config->enabled;
	}
	
	public function addNotifications(EmpevalUsers $user, EmpevalViewBgfWithSuperiorPositionId $bonusGoalsForm, EmpevalRejections $rejection) {
		if (!$this->enabled)
			return;
		$tpl = EmpevalNotificationTemplatesQuery::create()->findOneByName(EmpevalNotificationTemplates::REJECTION);
		if (!($tpl instanceof EmpevalNotificationTemplates))
			throw new Empeval_Exception_App(Empeval_Exception_Codes::ERROR_TPL_NOT_EXIST, array(EmpevalNotificationTemplates::REJECTION));
		$contentText = $this->addContentData($tpl->getContentText(), $user, $bonusGoalsForm, $rejection);
		$contentHtml = $this->addContentData($tpl->getContentHtml(), $user, $bonusGoalsForm, $rejection);
		
		$hrUsers = EmpevalUsersQuery::create()
		->useUserRolesRelatedByToUserIdQuery()
			->useRolesQuery()
				->filterByCode(EmpevalRoles::CODE_REJECTION_SERVICES)
			->endUse()
		->endUse()
		->find();
		$superiorUser = $bonusGoalsForm->getUserPositionsRelatedBySuperiorUserPositionId()->getUsers();
		
		$con = Propel::getConnection('vgp_empeval');
		$con->beginTransaction();
		try {
			$notification = new EmpevalNotifications();
			$notification->setStatus(EmpevalNotifications::STATUS_TO_SEND);
			$notification->setNotificationTemplates($tpl);
			$notification->save($con);
			
			$notificationData = new EmpevalNotificationData();
			$notificationData->setSubject($this->getSubject($tpl->getSubject(), $user));
			$notificationData->setContentText($contentText);
			$notificationData->setContentHtml($contentHtml);
			$notificationData->setNotifications($notification);
			$notificationData->save($con);

			foreach ($hrUsers as $user) {
				$notificationUser = new EmpevalNotificationUsers();
				$notificationUser->setUsers($user);
				$notificationUser->setNotifications($notification);
				$notificationUser->setEmail($user->getEmail());
				$notificationUser->save($con);
			}
			$notificationUser = new EmpevalNotificationUsers();
			$notificationUser->setUsers($superiorUser);
			$notificationUser->setNotifications($notification);
			$notificationUser->setEmail($superiorUser->getEmail());
			$notificationUser->save($con);
			
			$con->commit();
		} catch (Exception $e) {
			$con->rollBack();
			throw $e;
		}
	}
	
	private function getSubject($subject, EmpevalUsers $employee) {
		$subject = str_replace('{$fname}', $employee->getFname(), $subject);
		$subject = str_replace('{$lname}', $employee->getLname(), $subject);
		return $subject;
	}
	
	private function addContentData($content, EmpevalUsers $user, EmpevalViewBgfWithSuperiorPositionId $bonusGoalsForm, EmpevalRejections $rejection) {
		$content = str_replace('{$fname}', $user->getFname(), $content);
		$content = str_replace('{$lname}', $user->getLname(), $content);
		$content = str_replace('{$content}', $rejection->getText(), $content);
		$content = str_replace('{$link}', Empeval_Config::getInstance()->baseUrl . sprintf($this->config->data->bonusGoalsFormUrl, $bonusGoalsForm->getId()), $content);
		return $content;
	}
	
}